import 'dart:developer';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:video_player/pigeon_liviservice.dart';
import 'package:video_player/pigeon_videoplayer.dart';
import 'package:video_player_example/provider_menu_list.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var _platformVersion = "unkonw";
  static const MethodChannel _methodChannel =
      const MethodChannel('video_player');

  var _menuInfos = <MenuInfo>[];

  var _liviBridgeService = LiviBridgeServiceApi();

  @override
  void initState() {
    super.initState();
    MenuListProvider.instance.getMenuList2().then((value) {
      _menuInfos.addAll(value);
    });
    platformVersion.then((value) {
      log("value: $value");
      setState(() {
        _platformVersion = value;
      });
    });

    testPigeon().then((value) {
      log("message: ${value.result}");
    });
  }

  static Future<String> get platformVersion async {
    final String version =
        await _methodChannel.invokeMethod('getPlatformVersion');
    return version;
  }

  static Future<SearchReply> testPigeon() async {
    SearchRequest request = SearchRequest()..query = "requestMessage";
    var api = Api();
    var reply = await api.search(request);
    return reply;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('插件测试'),
        ),
        body: Container(
          child: Column(
            children: [
              Text('Running on: $_platformVersion\n'),
              buildTestListView()
            ],
          ),
        ),
      ),
    );
  }

  buildTestListView() {
    return ListView.separated(
        shrinkWrap: true,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text("${_menuInfos[index].title}"),
            onTap: () {
              onTabClick(index);
            },
          );
        },
        separatorBuilder: (context, index) {
          return Divider();
        },
        itemCount: _menuInfos.length);
  }

  onTabClick(index) {
    log("message: $index");
    var menu = _menuInfos[index];
    MethodType? type = menu.type;
    switch (type) {
      case MethodType.CHECKINSTALLAPPS:
        log("message title: ${menu.funcName}");
        _liviBridgeService.CheckInstallApps("arg_appName").then((value) {
          log("CHECKINSTALLAPPS result msg: $value");
        });
        break;
      case MethodType.GETSCANCODE:
        log("message title: ${menu.funcName}");
        _liviBridgeService.getScanCode().then((value) {
          log("GETSCANCODE result msg: $value");
        });
        break;
      case MethodType.GOCONTACTRESFORALLINFO:
        log("message title: ${menu.funcName}");
        _liviBridgeService.goContactResForAllInfo().then((value) {
          log("GOCONTACTRESFORALLINFO result msg:");
        });
        break;
      case MethodType.GOCONTACTRES:
        log("message title: ${menu.funcName}");
        _liviBridgeService.goContactRes().then((value) {
          log("GOCONTACTRES result msg:");
        });
        break;
      case MethodType.GETPHONESBYNUMBER:
        log("message title: ${menu.funcName}");
        _liviBridgeService.getPhonesByNumber("arg_phoneNumber").then((value) {
          log("GOCONTACTRES result msg: $value");
        });
        break;
      case MethodType.ONCLICK4JS:
        log("message title: ${menu.funcName}");
        _liviBridgeService.onClick4Js().then((value) {
          log("ONCLICK4JS result msg: $value");
        });
        break;
      case MethodType.GETVBPAYRESIMAGE:
        log("message title: ${menu.funcName}");
        _liviBridgeService.getVBPayResImage().then((value) {
          log("GETVBPAYRESIMAGE result msg: $value");
        });
        break;
      case MethodType.SHAREBYSDK:
        log("message title: ${menu.funcName}");
        _liviBridgeService.shareBySDK().then((value) {
          log("SHAREBYSDK result msg: $value");
        });
        break;
      case MethodType.HIDEKEYBOARD:
        log("message title: ${menu.funcName}");
        _liviBridgeService.hideKeyBoard().then((value) {
          log("HIDEKEYBOARD result msg: $value");
        });
        break;
    }
  }
}
